package com.campuscarbooking.api.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campuscarbooking.api.model.BusSchedule;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface BusScheduleMapper extends BaseMapper<BusSchedule> {

    /**
     * 查询指定路线方向、固定班车标识和状态的班车信息
     */
    @Select("SELECT bs.* FROM bus_schedule bs WHERE bs.route_direction = #{routeDirection} " +
            "AND bs.is_fixed = #{isFixed} AND bs.status = #{status}")
    IPage<BusSchedule> findByRouteDirectionAndIsFixedAndStatus(
            Page<?> page,
            @Param("routeDirection") String routeDirection,
            @Param("isFixed") Boolean isFixed,
            @Param("status") String status);

    /**
     * 查询指定时间段内可用的班车信息
     */
    @Select("SELECT bs.* FROM bus_schedule bs WHERE bs.route_direction = #{routeDirection} " +
            "AND bs.is_fixed = #{isFixed} AND bs.departure_time >= #{startTime} " +
            "AND bs.departure_time <= #{endTime} AND bs.status = 'active'")
    List<BusSchedule> findAvailableSchedules(
            @Param("routeDirection") String routeDirection,
            @Param("isFixed") Boolean isFixed,
            @Param("startTime") LocalTime startTime,
            @Param("endTime") LocalTime endTime);

    /**
     * 查询指定班车的预约数量（未取消）
     */
    @Select("SELECT COUNT(*) FROM reservations r WHERE r.schedule_id = #{scheduleId} AND r.status != 'canceled'")
    Integer countReservationsByScheduleId(@Param("scheduleId") Long scheduleId);

    /**
     * 查询指定时间段内的班车信息，按照发车时间排序
     */
    @Select("SELECT bs.* FROM bus_schedule bs WHERE bs.route_direction = #{routeDirection} " +
            "AND bs.departure_time >= #{startTime} AND bs.departure_time <= #{endTime} " +
            "AND bs.status = 'active' ORDER BY bs.departure_time ASC")
    List<BusSchedule> findByRouteDirectionAndTimePeriod(
            @Param("routeDirection") String routeDirection,
            @Param("startTime") LocalTime startTime,
            @Param("endTime") LocalTime endTime);

    /**
     * 查询班车信息及预约数量
     */
    List<Map<String, Object>> findSchedulesWithReservationCount(
            @Param("routeDirection") String routeDirection,
            @Param("isFixed") Boolean isFixed,
            @Param("status") String status,
            @Param("startTime") LocalTime startTime,
            @Param("endTime") LocalTime endTime);

    /**
     * 按时间段查询可用班车及座位信息
     */
    List<Map<String, Object>> findAvailableSchedulesByTimePeriod(
            @Param("routeDirection") String routeDirection,
            @Param("startTime") LocalTime startTime,
            @Param("endTime") LocalTime endTime);

    /**
     * 获取班车统计信息
     */
    List<Map<String, Object>> getBusScheduleStats(
            @Param("startTime") LocalTime startTime,
            @Param("endTime") LocalTime endTime,
            @Param("routeDirection") String routeDirection);

    /**
     * 查询指定班车详情及预约情况
     */
    Map<String, Object> findScheduleDetailById(@Param("scheduleId") Long scheduleId);
} 